package dao.admin.impl;

import dao.BaseDao;
import dao.admin.ManagerAddDao;
import entity.Manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ManagerAddDaoImpl
 *
 * @author ChenYanMei
 * @version 2023/9/5
 */
public class ManagerAddDaoImpl extends BaseDao implements ManagerAddDao {
    /**
     * 添加管理员信息
     * @param manager
     * @return
     */
    @Override
    public int add(Manager manager, String account) {
        int count=0;// 是否有重复的账号
        Connection conn = super.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "insert into manager(`name`, account, `password`, email) VALUES(?,?,?,?)";
        // select count(*) as count from manager where account=?
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            rs = pstmt.executeQuery();
            if (rs.next()){
                count = rs.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 添加信息
       if (count==0){
           return super.executeUpdate(sql, manager.getName(), manager.getAccount(), manager.getPassword(), manager.getEmail());
       }else {
           return count;
       }
    }
}
